define(['zepto', 'butterfly/view', '../../bsl',
    'text!./webview.html'
], function($, View, bsl, webview) {
    var me = null;

    var view = View.extend({
        events: {

            // ！！！！ 由于 引入 touch.js 后，tap事件会变成双击，所以 本页面内不能使用tap，用click代替。
            "tap #openDoc": "OpenDoc",
            "tap #back": "back",
            "tap .pclicked": "pclicked",
            "tap #webview_animation": "webview_animation",
            "tap #webview_embed": "webview_embed",
            "tap #floatWebview": "floatWebview",
            "tap #customeRefresh": "customeRefresh",
            "tap #webview_mask": "webview_mask",

        },
        render: function() {
            me = this;
        },

        onShow: function() {

            // 绑定自定义事件 滑动切换日历等  
            this.plusReady();


        },
        preate: {},
        preateWebivew: function(id) {
            if (!this.preate[id]) {
                var w = bsl.webview.create(id, id, {
                    scrollIndicator: 'none',
                    scalable: false,
                    popGesture: 'hide'
                },{preate: true});
                this.preate[id] = w;
                w.addEventListener('close', function() { //页面关闭后可再次打开
                    this._openw = null;
                    this.preate[id] && (this.preate[id] = null); //兼容窗口的关闭
                }, false);
            }
        },
        // 判断预载打开
        _openw: null,
        pclicked: function(id) {
            if (this._openw) {
                return;
            }
            this._openw = this.preate[id];
            if (this._openw) {
                if (this._openw.showded) {
                    this._openw.show('auto');
                } else {
                    this._openw.show(as);
                    this._openw.showded = true;
                }
                this._openw = null;
            } else {
                this._openw = bsl.webview.create(id, id, {
                    scrollIndicator: 'none',
                    scalable: false
                });
                this.preate[id] = this._openw;
                this._openw.addEventListener('loaded', function() { //叶面加载完成后才显示
                    //          setTimeout(function(){
                    this._openw.show(as);
                    this._openw.showded = true;
                    this._openw = null;
                    //          },10);
                }, false);
                this._openw.addEventListener('close', function() { //页面关闭后可再次打开
                    this._openw = null;
                    this.preate[id] && (this.preate[id] = null); //兼容窗口的关闭
                }, false);
            }
        },
        // H5 plus事件处理
        as: 'pop-in', // 默认动画类型
        plusReady: function() {
            //  this.preateWebivew('webview_animation.html');
            //  this.preateWebivew('webview_pullhead.html');
        },

        // 创建悬浮窗口
        floatw: null,
        floatWebview: function() {
            if (this.floatw) { // 避免快速多次点击创建多个窗口
                return;
            }
            this.floatw = bsl.webview.create("webview_float.html", "webview_float.html", {
                width: '200px',
                height: '200px',
                margin: "auto",
                background: "rgba(0,0,0,0.8)",
                scrollIndicator: 'none',
                scalable: false,
                popGesture: 'none'
            });
            this.floatw.addEventListener("loaded", function() {
                this.floatw.show('fade-in', 300);
                this.floatw = null;
            }, false);
        },
        // 创建自定义下拉刷新窗口
        customeRefresh: function() {
            if (bsl.os.name != "Android") {
                compatibleConfirm();
            } else {
                clicked('page/webview_pullhead_custom.html', true, true);
            }
        },

        back: function(e) {
            window.history.go(-1);
        },

        OpenDoc: function(e) {

            window.butterfly.navigate('com.no5.cordovademo/doc/webview', {
                trigger: true
            });
        },
        //跳转到相应页面
        webview_animation: function() {
           // this.pclicked('page/webview_animation');
            window.butterfly.navigate('com.no5.cordovademo/page/webview_animation', {
                trigger: true
            });
        },

        webview_embed: function() {
            window.butterfly.navigate('com.no5.cordovademo/page/webview_embed', {
                trigger: true
            });
        },


        webview_mask: function() {
            window.butterfly.navigate('com.no5.cordovademo/page/webview_mask', {
                trigger: true
            });
        },


    });
    return view;
});
